﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using NHibernate.Metadata;
using System.Reflection;
using Iesi.Collections.Generic;
using Iesi.Collections;
using System.Collections;

namespace Cms.Web.CmsSystem
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Submit : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string FormID = EntLib.Request.GetString("formid");
            string Action = EntLib.Request.GetString("action");
            //object objJsonData = EntLib.Request.GetString("jsondata");
            //开始处理前台提交的数据.
            if (String.IsNullOrEmpty(Action))
                Action = "update";
            switch (Action.ToLower())
            {
                case "update":
                    SaveDataByNHibernate(FormID, Action);
                    break;
                case "delete":
                    string recordid = EntLib.Request.GetString("recordid");
                    DeleteFormRecord(FormID, recordid);
                    break;
                default:
                    break;
            }
        }


        private void DeleteFormRecord(string formid, string recordid)
        {
            string ErrMsg = "";
            if (!String.IsNullOrEmpty(recordid))
            {
                try
                {
                    Type type = Core.GetModelTypeByFormID(formid);
                    Type blltype = typeof(Cms.BLL.BaseDao<,>);
                    Type TypeBll = blltype.MakeGenericType(type, typeof(string));
                    object bll = Activator.CreateInstance(TypeBll);
                    //object objModel = TypeBll.GetMethod("GetById").Invoke(bll, new object[] { recordid, false });
                    TypeBll.GetMethod("DeleteById").Invoke(bll, new object[] { recordid });
                    TypeBll.GetMethod("CommitChanges").Invoke(bll, null);
                }
                catch (Exception e)
                {
                    ErrMsg = e.Message;
                    EntLib.LogHelper.GetInstance().Error(e);
                }
            }
            if (String.IsNullOrEmpty(ErrMsg))
                EntLib.Response.ResponseText("{ Result:true}");
            else
                EntLib.Response.ResponseText("{Result:false}");
        }

        /// <summary>
        /// 利用NHibernate 保存数据
        /// </summary>
        /// <param name="formid"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        private bool SaveDataByNHibernate(string formid, string action)
        {
            bool result = false;
            Type type = Core.GetModelTypeByFormID(formid);
            System.Collections.Specialized.NameValueCollection DictMain = HttpContext.Current.Request.Form;//(Dictionary<string, object>)obj;                        
            object objModel = EntLib.Utiltiy.DictToModel(DictMain, type);
            if (objModel != null)
            {
                try
                {
                    Type TypeBll = Core.GetBLLTypeByFormID(formid, type);
                    object bll = Activator.CreateInstance(TypeBll);
                    TypeBll.GetMethod("SaveOrUpdate").Invoke(bll, new object[] { objModel });
                    TypeBll.GetMethod("CommitChanges").Invoke(bll, null);
                }
                catch (Exception e) {
                    EntLib.LogHelper.GetInstance(this.GetType()).Error("保存错误", e);
                }
            }
            return result;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
